package com.ruoyi.business.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.business.domain.BizGoods;
import com.ruoyi.business.domain.vo.FamilyDto;
import com.ruoyi.business.mapper.BizGoodsMapper;
import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.organization.domain.BizFamily;
import com.ruoyi.organization.domain.BizFamilyScoreLog;
import com.ruoyi.organization.domain.BizPeople;
import com.ruoyi.organization.mapper.BizFamilyMapper;
import com.ruoyi.organization.mapper.BizFamilyScoreLogMapper;
import com.ruoyi.organization.mapper.BizPeopleMapper;
import com.ruoyi.organization.service.impl.BizPeopleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BizPeopleGoodsMapper;
import com.ruoyi.business.domain.BizPeopleGoods;
import com.ruoyi.business.service.IBizPeopleGoodsService;

/**
 * 商品记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-15
 */
@Service
public class BizPeopleGoodsServiceImpl implements IBizPeopleGoodsService 
{
    @Autowired
    private BizPeopleGoodsMapper bizPeopleGoodsMapper;

    @Autowired
    private BizGoodsMapper bizGoodsMapper;

    @Autowired
    private BizPeopleMapper bizPeopleMapper;

    @Autowired
    private BizFamilyMapper bizFamilyMapper;

    @Autowired
    private BizFamilyScoreLogMapper bizFamilyScoreLogMapper;
    @Autowired
    private BizPeopleServiceImpl bizPeopleServiceImpl;

    /**
     * 查询商品记录
     * 
     * @param userGoodsId 商品记录主键
     * @return 商品记录
     */
    @Override
    public BizPeopleGoods selectBizPeopleGoodsByUserGoodsId(Long userGoodsId)
    {
        return bizPeopleGoodsMapper.selectBizPeopleGoodsByUserGoodsId(userGoodsId);
    }

    /**
     * 查询商品记录列表
     * 
     * @param bizPeopleGoods 商品记录
     * @return 商品记录
     */
    @Override
    @CommunityScope
    public List<BizPeopleGoods> selectBizPeopleGoodsList(BizPeopleGoods bizPeopleGoods)
    {
        if (StrUtil.isNotEmpty(bizPeopleGoods.getMobile())) {
            BizPeople bizPeople = new BizPeople();
            bizPeople.setPeopleMobile(bizPeopleGoods.getMobile());
            List<BizPeople> people = bizPeopleMapper.selectBizPeopleList(bizPeople);
            if (Objects.nonNull(people)) {
                bizPeopleGoods.setPeopleId(people.get(0).getPeopleId());
            }
        }
        return bizPeopleGoodsMapper.selectBizPeopleGoodsList(bizPeopleGoods);
    }

    /**
     * 新增商品记录
     * 
     * @param bizPeopleGoods 商品记录
     * @return 结果
     */
    @Override
    public int insertBizPeopleGoods(BizPeopleGoods bizPeopleGoods)
    {
        bizPeopleGoods.setCreateTime(DateUtils.getNowDate());
        return bizPeopleGoodsMapper.insertBizPeopleGoods(bizPeopleGoods);
    }

    /**
     * 修改商品记录
     * 
     * @param bizPeopleGoods 商品记录
     * @return 结果
     */
    @Override
    public int updateBizPeopleGoods(BizPeopleGoods bizPeopleGoods)
    {
        bizPeopleGoods.setUpdateTime(DateUtils.getNowDate());
        return bizPeopleGoodsMapper.updateBizPeopleGoods(bizPeopleGoods);
    }

    /**
     * 批量删除商品记录
     * 
     * @param userGoodsIds 需要删除的商品记录主键
     * @return 结果
     */
    @Override
    public int deleteBizPeopleGoodsByUserGoodsIds(Long[] userGoodsIds)
    {
        return bizPeopleGoodsMapper.deleteBizPeopleGoodsByUserGoodsIds(userGoodsIds);
    }

    /**
     * 删除商品记录信息
     * 
     * @param userGoodsId 商品记录主键
     * @return 结果
     */
    @Override
    public int deleteBizPeopleGoodsByUserGoodsId(Long userGoodsId)
    {
        return bizPeopleGoodsMapper.deleteBizPeopleGoodsByUserGoodsId(userGoodsId);
    }

    /**
     * 待兑换的商品
     *
     * @param status
     * @return
     */
    @Override
    public long selectBizPeopleGoodsByStatus(Integer status) {
        return bizPeopleGoodsMapper.selectBizPeopleGoodsByStatus(status);
    }

    /**
     * 取消兑换记录
     *
     * @param userGoodsIds
     * @return
     */
    @Override
    public int cancelExchange(Long userGoodsIds) {
        BizPeopleGoods bizPeopleGoods = bizPeopleGoodsMapper.selectBizPeopleGoodsByUserGoodsId(userGoodsIds);
        if (Objects.isNull(bizPeopleGoods)) {
            throw new ServiceException("兑换订单不存在");
        }
        if (bizPeopleGoods.getStatus().equals(2)) {
            throw new ServiceException("兑换订单已取消");
        }
        if (bizPeopleGoods.getStatus().equals(1)) {
            throw new ServiceException("兑换订单已完成");
        }
        //消耗的积分
        Long consumeScore = bizPeopleGoods.getExchangeNum();
        //兑换的资源
        Long sourceId = bizPeopleGoods.getSourceId();
        //查看兑换的资源是否现在库存
        BizGoods bizGoods = bizGoodsMapper.selectBizGoodsByGoodsId(sourceId);

        // 兑换的用户 归还用户积分
        Long peopleId = bizPeopleGoods.getPeopleId();
        BizPeople bizPeople = bizPeopleMapper.selectBizPeopleByPeopleId(peopleId);
        if (Objects.isNull(bizPeople)) {
            throw new ServiceException("兑换用户不存在");
        }
        BizFamily bizFamily = bizFamilyMapper.findByHeadCard(bizPeople.getHeadCard());
        if (Objects.isNull(bizFamily)) {
            throw new ServiceException("兑换用户未开通家庭积分");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //增加商品退还记录
        BizFamilyScoreLog bizFamilyScoreLog = new BizFamilyScoreLog();
        String orderSerialNo = "JF" + IdUtil.getSnowflakeNextIdStr();
        bizFamilyScoreLog.setOrderSerialNo(orderSerialNo);
        bizFamilyScoreLog.setOrderNo(orderSerialNo);
        bizFamilyScoreLog.setPeopleId(loginUser.getUser().getUserId());
        bizFamilyScoreLog.setFamilyId(bizFamily.getFamilyId());
        bizFamilyScoreLog.setOrderScoreType(1l);
        bizFamilyScoreLog.setOrderType(6l);
        bizFamilyScoreLog.setChangeScore(consumeScore);
        bizFamilyScoreLog.setTotalScore(bizFamily.getFamilyScore());
        bizFamilyScoreLog.setRemark("商品退还:"+bizPeopleGoods.getTitle());
        bizFamilyScoreLog.setCreateBy(loginUser.getUser().getUserName());
        bizFamilyScoreLog.setCreateTime(DateUtils.getNowDate());

        bizFamilyScoreLogMapper.insertBizFamilyScoreLog(bizFamilyScoreLog);
        bizFamilyMapper.increaseScore(consumeScore,bizFamily.getFamilyId());
        //更新库存
        if (bizGoods.getStockType().equals(1)) {
            bizGoodsMapper.increaseStock(sourceId,1l);
        }
        bizPeopleGoods.setStatus(2l);
        bizPeopleGoods.setUpdateBy(loginUser.getUsername());
        bizPeopleGoods.setUpdateTime(DateUtils.getNowDate());
        bizPeopleGoodsMapper.updateBizPeopleGoods(bizPeopleGoods);
        return 1;
    }

    /**
     * 导出格式化导出数据
     *
     * @param list
     * @return
     */
    @Override
    public List<BizPeopleGoods> export(List<BizPeopleGoods> list) {

        for (BizPeopleGoods bizPeopleGoods : list) {

            FamilyDto familyDto = bizPeopleServiceImpl.findFamlily(bizPeopleGoods.getPeopleId());
            bizPeopleGoods.setHeaderPeople(familyDto.getHeaderPeople());
            bizPeopleGoods.setFamily(familyDto.getFamily());
            bizPeopleGoods.setExchangeTime(dateToString(bizPeopleGoods.getExchangeStartTime())+"-"+dateToString(bizPeopleGoods.getExchangeEndTime()));
            bizPeopleGoods.setStatusTxt(filterStatus(bizPeopleGoods.getStatus()));
        }
        return list;
    }

    private String filterStatus(Long status) {
        if (status.equals(0l)) {
            return "待领取";
        } else if (status.equals(1l)) {
            return "已领取";
        } else if (status.equals(2l)) {
            return "取消兑换";
        } else {
            return "未知";
        }
    }

    private String dateToString(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}
